package m.jclecai.com.activity;

import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.alibaba.fastjson.JSONObject;
import com.squareup.okhttp.Request;

import java.io.IOException;
import java.util.HashMap;

import butterknife.BindView;
import butterknife.OnClick;
import m.jclecai.com.R;
import m.jclecai.com.base.BaseActivity;
import m.jclecai.com.bean.ProofBean;
import m.jclecai.com.constant.Constant_Url;
import m.jclecai.com.utils.BaseUtils;
import m.jclecai.com.utils.ChannelUtil;
import m.jclecai.com.utils.MyToast;
import m.jclecai.com.utils.http.FastOk;
import m.jclecai.com.utils.http.HttpManager;

/**
 * Created by ZD on 2017/5/18.
 */

public class PasswordValidateActivity extends BaseActivity {

    private String phone, userName;

    @BindView(R.id.et_text)
    EditText etText;

    @BindView(R.id.tv_repeat)
    TextView tv_repeat;

    @Override
    public int getContentView() {
        return R.layout.activity_validate;
    }

    @Override
    public void initData() {
        super.initData();
        phone = getIntent().getStringExtra("phone");
        userName = getIntent().getStringExtra("userName");
        String title = getIntent().getStringExtra("title");
        if (TextUtils.isEmpty(title))
            title = "忘记密码";
        setTitle(title);
        leftOptionText("");
        BaseUtils.countDowm(this, 60, tv_repeat);
    }

    @OnClick(R.id.tv_submit)
    void submit() {
        if (TextUtils.isEmpty(userName)) {
            resetPwdByPhone();
        } else {
            resetPwdByPhoneAndName();
        }
    }

    @OnClick(R.id.tv_repeat)
    void repeat() {
        BaseUtils.countDowm(this, 60, tv_repeat);
        //如果是从用户名跳转
        if (TextUtils.isEmpty(userName)) {
            getUserNameByPhone();
        } else {
            getUserNameByPhoneAndName();
        }
    }

    //重置密码
    private void resetPwdByPhoneAndName() {
        String vphone = etText.getText().toString();
        if(TextUtils.isEmpty(vphone)){
            DisPlay("请输入您收到的信息");
            return;
        }
        showDialog();
        final HashMap<String, String> pair = new HashMap<>();
        pair.put("vphone", etText.getText().toString());//短信验证码
        pair.put("user", userName);//用户名
        pair.put("phone", phone);//绑定的手机号
        pair.put("sessionid", "");//图片验证码
        pair.put("password", "");//密码
        pair.put("flag", "2");//标志flag=2 (修改密码)flag=3 (绑定手机)flag=4 (修改并绑定手机)
        pair.put("appstore", ChannelUtil.getChannel());//标志flag=2 (修改密码)flag=3 (绑定手机)flag=4 (修改并绑定手机)
        HttpManager.postAsyn(Constant_Url.CHECK_PHONE_RANDNUM_URL, new HttpManager.StringCallback() {

            @Override
            public void onFailure(Request request, IOException e) {
                dissDialog();
                DisPlay(getString(R.string.error_network));
            }

            @Override
            public void onResponse(String response) {
                dissDialog();
                if (!TextUtils.isEmpty(response)) {
                    JSONObject obj = FastOk.get(response,JSONObject.class);
                    if (obj != null) {
                        if (obj.getIntValue("status") == 0) {
                            String password = obj.getString("password");
                            showAlertDialog("友情提示", "您的密码为:" + password,
                                    "知道了", "去登录", null, new View.OnClickListener() {
                                        @Override
                                        public void onClick(View v) {
                                            finish();
                                        }
                                    });
                        }else {
                            DisPlay(obj.getString("message"));
                        }
                    } else {
                        DisPlay(getString(R.string.error_network));
                    }
                }
            }
        }, pair);
    }

    //发送验证码
    private void getUserNameByPhone() {
        showDialog();
        HashMap<String, String> pair = new HashMap<>();
        pair.put("phone", phone);
        pair.put("sessionid", "");//图形验证码
        HttpManager.postAsyn(Constant_Url.FIND_NAME_BY_MOBILE, new HttpManager.StringCallback() {
            @Override
            public void onFailure(Request request, IOException e) {
                dissDialog();
                DisPlay(getString(R.string.error_network));
            }

            @Override
            public void onResponse(String response) {
                dissDialog();
                ProofBean proofBean = FastOk.get(response, ProofBean.class);
                if (proofBean != null)
                    MyToast.showToast(PasswordValidateActivity.this, proofBean.getMessage());
                else
                    DisPlay(getString(R.string.error_network));
            }
        }, pair);
    }


    //重置密码
    private void resetPwdByPhone() {
        String user = etText.getText().toString();
        if(TextUtils.isEmpty(user)){
            DisPlay("请输入您收到的信息");
            return;
        }
        showDialog();
        final HashMap<String, String> pair = new HashMap<>();
        pair.put("user", user);//用户名
        pair.put("phone", phone);//手机号码
        pair.put("flag", "2");//标志flag=2 (修改密码)flag=3 (绑定手机)flag=4 (修改并绑定手机)
        HttpManager.postAsyn(Constant_Url.RESET_PWD_BY_MOBILE, new HttpManager.StringCallback() {

            @Override
            public void onFailure(Request request, IOException e) {
                dissDialog();
                DisPlay(getString(R.string.error_network));
            }

            @Override
            public void onResponse(String response) {
                dissDialog();
                if (!TextUtils.isEmpty(response)) {
                    JSONObject obj = FastOk.get(response,JSONObject.class);
                    if (obj != null) {
                        if (obj.getIntValue("status") == 0) {
                            showAlertDialog("友情提示", "请以您收到的帐号密码进行登录",
                                    "知道了", "去登录", null, new View.OnClickListener() {
                                        @Override
                                        public void onClick(View v) {
                                            finish();
                                        }
                                    });
                        }else {
                            DisPlay(obj.getString("message"));
                        }
                    } else {
                        DisPlay(getString(R.string.error_network));
                    }
                }
            }
        }, pair);
    }

    //发送验证码
    private void getUserNameByPhoneAndName() {
        showDialog();
        HashMap<String, String> pair = new HashMap<>();
        pair.put("user", userName);
        pair.put("phone", phone);
        pair.put("sessionid", "");//图形验证码
        pair.put("flag", "2");
        HttpManager.postAsyn(Constant_Url.FIND_PWD_BY_MOBILE, new HttpManager.StringCallback() {
            @Override
            public void onFailure(Request request, IOException e) {
                dissDialog();
                DisPlay(getString(R.string.error_network));
            }

            @Override
            public void onResponse(String response) {
                dissDialog();
                ProofBean proofBean = FastOk.get(response, ProofBean.class);
                if (proofBean != null)
                    MyToast.showToast(PasswordValidateActivity.this, proofBean.getMessage());
                else
                    DisPlay(getString(R.string.error_network));
            }
        }, pair);
    }
}
